/**
 * Created by wenchuan on 2018/6/15 11:35.
 */
define(function () {

    var self,
        app = win.app,
        detailData,
        characteristics = [],
        gwcxData = {
            GWCX005: '0',
            GWCX006: '0',
            GWCX007: '0',
            GWCX008: '0'
        },
        comments = [],
        other = {
            GWAM016: '',
            GWAM017: ''
        },
        refreshData = {
            KEY: 0, //评价KEY
            STATUS: 0,
            COUNT: 0,
            COUNT1: 0,
            COUNT2: 0,
            COUNT3: 0,
            PV: 0,
            GWAM005: 0, //料号类型
        },
        updateData = {
            GWAM001: '',//料号
            GWCM003: 1,//数量
            GWAP100: 0,//单价
            GWAP100_1: 0,//实际价格
            GWBK008: '',//预计购买日期
            GWBK009: '',//预计回款日期
            GWAM015: '',//商品图片
            GWAML003: '',//料号简称
        },
        pageParameters,
        pageUi,
        isUpdate = false,
        _targer
    ;

    app.newModule({

        init: function () {
            self = this;
            self._super({
                _scroll: ['#scroll1','#scroll2'],
                _readyBefore: function () {
                    pageParameters = self._pageParameters;
                    detailData = pageParameters.item;
                    _targer = pageParameters._targer;
                    app.extend(updateData, detailData);
                    // 拜访记录--添加
                    if (pageParameters.last && pageParameters.last._targer == 'add') {

                    } else if (pageParameters._targer) {

                        // 拜访记录--修改
                        if (pageParameters._targer == 'update') {
                            isUpdate = true;
                        }
                    }
                }
            }, win);// 放在后边
        },
        getGwcxData: function () {
            app.ajax({
                xurl: "api/wx/gwat300/getGwcxData",
                // xtime: '1d',
                xdata: {
                    GWCX002: updateData.GWAM001
                },
                cb: function (result) {
                    if (result.ok && result.data.data != null) {
                        Vue.set(gwcxData, 'GWCX005', result.data.data.GWCX005);
                        Vue.set(gwcxData, 'GWCX006', result.data.data.GWCX006);
                        Vue.set(gwcxData, 'GWCX007', result.data.data.GWCX007);
                        Vue.set(gwcxData, 'GWCX008', result.data.data.GWCX008);
                    }
                }
            });
        },
        getGwcwCount: function () {
            app.ajax({
                xurl: "api/wx/gwat100/getGwcwCount",
                // xtime: '1d',
                xdata: {
                    GWCW020: updateData.GWAM001,
                    GWAM005: 1,//detailData.gwam005,
                    status: refreshData.status
                },
                cb: function (result) {
                    if (result.ok) {
                        var data = result.data.count;
                        app.extend(refreshData, result.data.count);
                        if (data.count > 0) {
                            refreshData.pv = (refreshData.count1 / refreshData.count * 100).toFixed(2);
                        } else {
                            refreshData.pv = 0;
                        }
                    }
                }
            });
        },
        getGwamOther: function () {
            app.ajax({
                xurl: "api/wx/gwat100/getGwamOther",
                // xtime: '1d',
                xdata: {
                    GWAM001: detailData.GWAM001
                },
                cb: function (result) {
                    if (result.ok) {
                        var data = result.data.data;
                        app.extend(other, data);
                    }
                }
            });
        },
        _initRefresh: function () { // 实例化刷新组件
            if (self._refreshIds.length == 0) {
                self._refreshIds = ['scroll3'];
                self._refreshVueOpts = {
                    data: {
                        others: refreshData
                    },
                    methods: {
                        statusChange: function () {
                            self._refresh();
                            self.getGwcwCount();
                        },
                        getpj: function (val) {
                            refreshData.key = val;
                            self._refresh();
                        }
                    }
                };
                this._super(win);
            } else {
                self._refresh();
            }
            self.getGwcwCount();
        },
        _refreshCb: function (goods) {// 刷新组件回调函授
            var _super = this._super;
            app.ajax({
                xurl: "api/wx/gwat100/getGwcw",
                // xtime: '1m',
                // ocache: true,
                xdata: {
                    page:goods.page+1,
                    limit: goods.limit,
                    GWCW020: updateData.GWAM001,
                    key: refreshData.key,//评论级别
                    GWAM005: 1, //料号类型
                    status: refreshData.status // 0全部 1当前商品评论用于子料号
                },
                cb: function (result) {
                    if (result.ok) {
                        _super({
                            obj: goods,
                            data: result.data.data,
                            nodata: '暂无评论信息...',
                            nodata_img: 'noneComment.png'
                        });
                    }
                }
            });
        },
        _lazy: function () {
            this._super();

            refreshData.GWAM005 = detailData.GWAM005;
            new Vue({
                el: app.getByid(doc, 'scroll2'),
                data: other,
                methods: {}
            });

            $('.wf-infoTab ul li').on('tap', function () {
                $(this).addClass('active').siblings('li').removeClass('active');
                $(this).parents('.wf-infoTab').find('.tabCont .group').eq($(this).index()).show().siblings('.group').hide();
            });

            doc.getElementById('slider').addEventListener('slide', function (e) {
                if (e.detail.slideNumber == 2) {
                    self._initRefresh(win);
                }
            });

            self.getGwamOther();

            if (_targer && _targer == 'read') {

            } else {
                $('#btnSave1Div').show();
                app.taps.call(win, [{
                    o: 'btnSave1',
                    cb: function (e) {
                        if (updateData.GWAP100_1 == '') {
                            app.alert({msg: '实际价格不能为空'});
                            return;
                        }
                        if (updateData.GWBK008 == '') {
                            app.alert({msg: '预计购买日期不能为空'});
                            return;
                        }
                        if (updateData.GWBK009 == '') {
                            app.alert({msg: '预计回款日期不能为空'});
                            return;
                        }

                        if (isUpdate) {
                            pageParameters.cb(updateData)
                        } else {
                            app.extend(detailData, updateData);
                            detailData.GWAM005 = 1;
                            detailData.GWAP100 = detailData.GWAP100_1;
                            pageParameters.last.cb(detailData);
                            app.back(1);
                        }

                    }
                }]);
            }

        },
        _ready: function () {
            var scroll1Vue = new Vue({
                el: app.getByid(doc, 'scroll1'),
                data: {
                    item: detailData,
                    upd: updateData,
                    characteristics: characteristics,
                    gwcxData: gwcxData,
                    comments: comments
                },
                watch: {
                    'upd.gwbk008': function (newVal, oldVal) {
                        if (updateData.GWBK009 == '') {
                            updateData.GWBK009 = newVal;
                        }
                    }
                },
                methods: {
                    minus: function (val) {
                        if (val.GWCM003 > 1) {
                            val.GWCM003--;
                        }
                    },
                    plus: function (val) {
                        val.GWCM003++;
                    }
                }
            });

            Vue.component('commentsitem', {
                template: app.getByid(doc, 'comments-template'),
                props: {
                    model: Array
                },
                methods: {}
            });

            app.getCharacteristic({
                gwan001_: updateData.GWAM001,
                gwam005_: detailData.GWAM005,
                _vue: scroll1Vue,
                cb: function (result) {
                    //用到缓存 会调用2次，不要放跟特征值无关的逻辑

                },
                changeCb: function (obj) {
                    updateData.GWAM001 = obj.GWAM001;
                    updateData.GWAML003 = obj.GWAML003;
                    updateData.GWAM008 = obj.GWAP004;
                    updateData.GWAP100 = (isNaN(Number(obj.GWAP100)) ? 0 : Number(obj.GWAP100));
                    if (updateData.GWAP100_1 == '0') {
                        updateData.GWAP100_1 = updateData.GWAP100;
                    } else {
                        updateData.GWAP100_1 = updateData.GWAP100_1;
                    }
                    updateData.GWAM015 = obj.GWAM015;
                    self.getGwcxData();
                    self.getGwamOther();
                }
            });
        }
    });

});